package controller;

import java.util.ArrayList;
//import java.util.Stack;

public class TokenParser {
	
	/** Needed for the ModelGenerator**/
	static ArrayList<ArrayList<TokensClass>> subArrays = new ArrayList<ArrayList<TokensClass>>();
	/*********************************/
	
	/**A return value of type ArrayList<ArrayList<TokensClass>> is needed for the ModelGenerator**/
	public static ArrayList<ArrayList<TokensClass>> parse(ArrayList<TokensClass> tokens){
		//the last token in the program is usually the EOF , it needs to be omitted
		subArrays.clear();
						/* I solved the EOF issue from inside
						 * the scanner so this next condition is 
						 * no longer needed*/
//		if(tokens.get(tokens.size()-1).getType().equals("EOF")){
//			tokens.remove(tokens.size()-1);
//		}
		//iterating through each code line at a time
		while(!tokens.isEmpty()){
			ArrayList<TokensClass> tokensPerLine = new ArrayList<TokensClass>();//tokens found in one line "delimiter: LBRACE and SEMICOLON and RBRACE"
			while(true){
				tokensPerLine.add(tokens.get(0));
				tokens.remove(0);//popping the copied element
				if( tokensPerLine.get(tokensPerLine.size()-1).getName().equals("{")
				 || tokensPerLine.get(tokensPerLine.size()-1).getName().equals(";")
				 || tokensPerLine.get(tokensPerLine.size()-1).getName().equals("}")) {
					break;//break out of the while(true) loop
				}
			}//end while(true)
			//displaying the tokensPerLine array list 
			for(TokensClass tokenAlias:tokensPerLine)
				System.out.printf("<%s>  ",tokenAlias.getName());
			System.out.println();
					
			/**Needed for the ModelGenerator**/
			subArrays.add(tokensPerLine);
			/*********************************/
			/* 							TODO								 *
			 *    parsing each line using stack(s) should be done here		 *
			 * 																 */
					
		}//end while(!tokens.isEmpty())
		/**Needed for the ModelGenerator**/
		return subArrays;
		/*********************************/
	}

}
